"""
Rtorrent web client with additional abilities

Rtorrent scgi can be used with following config :

  # lighttpd -Df lighttpd.rtorrent.conf

--- lighttd.rtorrent.conf ---
###############################################################################
# Konfig lighttpd.
###############################################################################

server.modules = ( "mod_scgi" )
server.port = 5001
server.pid-file = "/var/run/lighttpd-rtorrent.pid"
server.errorlog = "/var/log/lighttpd/rtorrent.error.log"
server.document-root = "/var/torrent/torrents"
scgi.server = ( "/" => ( "127.0.0.1" => ( "host" => "127.0.0.1", "port" => 5000, "check-local" => "disable")))
--- lighttd-rtorrent ---


"""

import xmlrpclib
import logging
from util import MWT
log = logging.getLogger("base")
from util import partial

from titles import get_title, get_year

def xmlrpc_partial(func, *args, **kwargs):
    """ Partial for xmlrpc with debug etc """
    def call_func(*fargs, **fkwargs):
        log.debug("Calling %s",func)
        d = ckwargs.copy()
        d.update(fkwargs)
	return fn(*(args + kwargs), **d)
    return call_func

def xmlrpc_call(func, *args, **kwargs):
    """ Wrapper for calling an xmlrpc function """
    log.debug("Calling %s", func)
    return func(*args, **kwargs)

class Torrent(object):
    """ A torrent file and download.

     Underlined methods are blocking TODO: explain
    """
    def __init__(self, server, torrent_id):
        self.server = server
        self.torrent_id = torrent_id
        log.info("Torrent %s initialized", self.torrent_id)

    # Left for debug only
    ##def _get_name(self):
        ##log.debug("Fetching name of torrent %s", self.torrent_id)
        ##return self.server.d.get_name(self.torrent_id)

    ##def _get_completness(self):
        ##r = (self.server.d.get_complete(self.torrent_id))
        ###if r == 0:
            ###r = 100
        ##return r

    ##def _get_is_open(self):
        ##""" """
        ###return self.server.f.get_is_open(self.torrent_id)

    ''' Poinizsze atrybuty wygenerowalem takim kodem :
#    attributes = [x for x in """ d.get_base_filename d.get_base_path d.get_bytes_done d.get_chunk_size d.get_chunks_hashed d.get_complete d.get_completed_bytes d.get_completed_chunks d.get_connection_current d.get_connection_leech d.get_connection_seed d.get_creation_date d.get_custom1 d.get_custom2 d.get_custom3 d.get_custom4 d.get_custom5 d.get_directory d.get_down_rate d.get_down_total d.get_free_diskspace d.get_hash d.get_hashing d.get_ignore_commands d.get_left_bytes d.get_local_id d.get_local_id_html d.get_max_file_size d.get_max_peers d.get_max_uploads d.get_message d.get_min_peers d.get_mode d.get_name d.get_peer_exchange d.get_peers_accounted d.get_peers_complete d.get_peers_connected d.get_peers_max d.get_peers_min d.get_peers_not_connected d.get_priority d.get_priority_str d.get_ratio d.get_size_bytes d.get_size_chunks d.get_size_files d.get_skip_rate d.get_skip_total d.get_state d.get_state_changed d.get_tied_to_file d.get_tracker_focus d.get_tracker_numwant d.get_tracker_size d.get_up_rate d.get_up_total d.get_uploads_max d.is_active d.is_hash_checked d.is_hash_checking d.is_multi_file d.is_open d.is_private""".split()]
    attributes = [x for x in """ d.get_base_filename d.get_base_path d.get_bytes_done d.get_chunk_size d.get_chunks_hashed d.get_complete d.get_completed_bytes d.get_completed_chunks d.get_connection_current d.get_connection_leech d.get_connection_seed d.get_creation_date d.get_custom1 d.get_custom2 d.get_custom3 d.get_custom4 d.get_custom5 d.get_directory d.get_down_rate d.get_down_total d.get_free_diskspace d.get_hash d.get_hashing d.get_ignore_commands d.get_left_bytes d.get_local_id d.get_local_id_html d.get_max_file_size d.get_max_peers d.get_max_uploads d.get_message d.get_min_peers d.get_mode d.get_name d.get_peer_exchange d.get_peers_accounted d.get_peers_complete d.get_peers_connected d.get_peers_max d.get_peers_min d.get_peers_not_connected d.get_priority d.get_priority_str d.get_ratio d.get_size_bytes d.get_size_chunks d.get_size_files d.get_skip_rate d.get_skip_total d.get_state d.get_state_changed d.get_tied_to_file d.get_tracker_focus d.get_tracker_numwant d.get_tracker_size d.get_up_rate d.get_up_total d.get_uploads_max d.is_active d.is_hash_checked d.is_hash_checking d.is_multi_file d.is_open d.is_private""".split()]

    def resof(s):
    if s.startswith('d.is_'):
        return s[2:] #5:]
    else:
        return s[6:]
    for x in sorted(attributes):
        print S % (resof(x), x)
    '''

    def guess_title(self):
        return get_title(self.name)

    def guess_year(self):
        return get_year(self.name)

    # defines d.check_hash   # defined by d.check_hash - check_hash
    # defines d.close   # defined by d.close - close
    # defines d.delete_link   # defined by d.delete_link - delete_link
    # defines d.delete_tied   # defined by d.delete_tied - delete_tied
    # defines d.erase   # defined by d.erase - erase
    base_filename       = property(MWT()( lambda self: self.server.d.get_base_filename(self.torrent_id)))   # defined by d.get_base_filename - base_filename
    base_path           = property(MWT()( lambda self: self.server.d.get_base_path(self.torrent_id)))   # defined by d.get_base_path - base_path
    bytes_done          = property(MWT()( lambda self: self.server.d.get_bytes_done(self.torrent_id)))   # defined by d.get_bytes_done - bytes_done
    chunk_size          = property(MWT()( lambda self: self.server.d.get_chunk_size(self.torrent_id)))   # defined by d.get_chunk_size - chunk_size
    chunks_hashed       = property(MWT()( lambda self: self.server.d.get_chunks_hashed(self.torrent_id)))   # defined by d.get_chunks_hashed - chunks_hashed
    complete            = property(MWT()( lambda self: self.server.d.get_complete(self.torrent_id)))   # defined by d.get_complete - complete
    completed_bytes     = property(MWT()( lambda self: self.server.d.get_completed_bytes(self.torrent_id)))   # defined by d.get_completed_bytes - completed_bytes
    completed_chunks    = property(MWT()( lambda self: self.server.d.get_completed_chunks(self.torrent_id)))   # defined by d.get_completed_chunks - completed_chunks
    connection_current  = property( MWT()( lambda self: self.server.d.get_connection_current(self.torrent_id)), lambda self: self.server.d.set_connection_current(self.torrent_id))   # defined by d.get_connection_current - connection_current
    connection_leech    = property( MWT()( lambda self: self.server.d.get_connection_leech(self.torrent_id)), lambda self: self.server.d.set_connection_leech(self.torrent_id))   # defined by d.get_connection_leech - connection_leech
    connection_seed     = property( MWT()( lambda self: self.server.d.get_connection_seed(self.torrent_id)), lambda self: self.server.d.set_connection_seed(self.torrent_id))   # defined by d.get_connection_seed - connection_seed
    creation_date       = property(MWT()( lambda self: self.server.d.get_creation_date(self.torrent_id)))   # defined by d.get_creation_date - creation_date
    custom1             = property( MWT()( lambda self: self.server.d.get_custom1(self.torrent_id)), lambda self: self.server.d.set_custom1(self.torrent_id))   # defined by d.get_custom1 - custom1
    custom2             = property( MWT()( lambda self: self.server.d.get_custom2(self.torrent_id)), lambda self: self.server.d.set_custom2(self.torrent_id))   # defined by d.get_custom2 - custom2
    custom3             = property( MWT()( lambda self: self.server.d.get_custom3(self.torrent_id)), lambda self: self.server.d.set_custom3(self.torrent_id))   # defined by d.get_custom3 - custom3
    custom4             = property( MWT()( lambda self: self.server.d.get_custom4(self.torrent_id)), lambda self: self.server.d.set_custom4(self.torrent_id))   # defined by d.get_custom4 - custom4
    custom5             = property( MWT()( lambda self: self.server.d.get_custom5(self.torrent_id)), lambda self: self.server.d.set_custom5(self.torrent_id))   # defined by d.get_custom5 - custom5
    directory           = property( MWT()( lambda self: self.server.d.get_directory(self.torrent_id)), lambda self: self.server.d.set_directory(self.torrent_id))   # defined by d.get_directory - directory
    down_rate           = property(MWT()( lambda self: self.server.d.get_down_rate(self.torrent_id)))   # defined by d.get_down_rate - down_rate
    down_total          = property(MWT()( lambda self: self.server.d.get_down_total(self.torrent_id)))   # defined by d.get_down_total - down_total
    free_diskspace      = property(MWT()( lambda self: self.server.d.get_free_diskspace(self.torrent_id)))   # defined by d.get_free_diskspace - free_diskspace
    hash                = property(MWT()( lambda self: self.server.d.get_hash(self.torrent_id)))   # defined by d.get_hash - hash
    hashing             = property(MWT()( lambda self: self.server.d.get_hashing(self.torrent_id)))   # defined by d.get_hashing - hashing
    ignore_commands     = property( MWT()( lambda self: self.server.d.get_ignore_commands(self.torrent_id)), lambda self: self.server.d.set_ignore_commands(self.torrent_id))   # defined by d.get_ignore_commands - ignore_commands
    left_bytes          = property(MWT()( lambda self: self.server.d.get_left_bytes(self.torrent_id)))   # defined by d.get_left_bytes - left_bytes
    local_id            = property(MWT()( lambda self: self.server.d.get_local_id(self.torrent_id)))   # defined by d.get_local_id - local_id
    local_id_html       = property(MWT()( lambda self: self.server.d.get_local_id_html(self.torrent_id)))   # defined by d.get_local_id_html - local_id_html
    max_file_size       = property( MWT()( lambda self: self.server.d.get_max_file_size(self.torrent_id)), lambda self: self.server.d.set_max_file_size(self.torrent_id))   # defined by d.get_max_file_size - max_file_size
    max_peers           = property( MWT()( lambda self: self.server.d.get_max_peers(self.torrent_id)), lambda self: self.server.d.set_max_peers(self.torrent_id))   # defined by d.get_max_peers - max_peers
    max_uploads         = property( MWT()( lambda self: self.server.d.get_max_uploads(self.torrent_id)), lambda self: self.server.d.set_max_uploads(self.torrent_id))   # defined by d.get_max_uploads - max_uploads
    message             = property( MWT()( lambda self: self.server.d.get_message(self.torrent_id)), lambda self: self.server.d.set_message(self.torrent_id))   # defined by d.get_message - message
    min_peers           = property( MWT()( lambda self: self.server.d.get_min_peers(self.torrent_id)), lambda self: self.server.d.set_min_peers(self.torrent_id))   # defined by d.get_min_peers - min_peers
    mode                = property(MWT()( lambda self: self.server.d.get_mode(self.torrent_id)))   # defined by d.get_mode - mode
    name                = property( MWT()( lambda self: self.server.d.get_name(self.torrent_id)), lambda self: self.server.d.set_name(self.torrent_id))   # defined by d.get_name - name
    peer_exchange       = property( MWT()( lambda self: self.server.d.get_peer_exchange(self.torrent_id)), lambda self: self.server.d.set_peer_exchange(self.torrent_id))   # defined by d.get_peer_exchange - peer_exchange
    peers_accounted     = property(MWT()( lambda self: self.server.d.get_peers_accounted(self.torrent_id)))   # defined by d.get_peers_accounted - peers_accounted
    peers_complete      = property(MWT()( lambda self: self.server.d.get_peers_complete(self.torrent_id)))   # defined by d.get_peers_complete - peers_complete
    peers_connected     = property(MWT()( lambda self: self.server.d.get_peers_connected(self.torrent_id)))   # defined by d.get_peers_connected - peers_connected
    peers_max           = property( MWT()( lambda self: self.server.d.get_peers_max(self.torrent_id)), lambda self: self.server.d.set_peers_max(self.torrent_id))   # defined by d.get_peers_max - peers_max
    peers_min           = property( MWT()( lambda self: self.server.d.get_peers_min(self.torrent_id)), lambda self: self.server.d.set_peers_min(self.torrent_id))   # defined by d.get_peers_min - peers_min
    peers_not_connected = property(MWT()( lambda self: self.server.d.get_peers_not_connected(self.torrent_id)))   # defined by d.get_peers_not_connected - peers_not_connected
    priority            = property( MWT()( lambda self: self.server.d.get_priority(self.torrent_id)), lambda self: self.server.d.set_priority(self.torrent_id))   # defined by d.get_priority - priority
    priority_str        = property(MWT()( lambda self: self.server.d.get_priority_str(self.torrent_id)))   # defined by d.get_priority_str - priority_str
    ratio               = property(MWT()( lambda self: self.server.d.get_ratio(self.torrent_id)))   # defined by d.get_ratio - ratio
    size_bytes          = property(MWT()( lambda self: self.server.d.get_size_bytes(self.torrent_id)))   # defined by d.get_size_bytes - size_bytes
    size_chunks         = property(MWT()( lambda self: self.server.d.get_size_chunks(self.torrent_id)))   # defined by d.get_size_chunks - size_chunks
    size_files          = property(MWT()( lambda self: self.server.d.get_size_files(self.torrent_id)))   # defined by d.get_size_files - size_files
    skip_rate           = property(MWT()( lambda self: self.server.d.get_skip_rate(self.torrent_id)))   # defined by d.get_skip_rate - skip_rate
    skip_total          = property(MWT()( lambda self: self.server.d.get_skip_total(self.torrent_id)))   # defined by d.get_skip_total - skip_total
    state               = property(MWT()( lambda self: self.server.d.get_state(self.torrent_id)))   # defined by d.get_state - state
    state_changed       = property(MWT()( lambda self: self.server.d.get_state_changed(self.torrent_id)))   # defined by d.get_state_changed - state_changed
    tied_to_file        = property(MWT()( lambda self: self.server.d.get_tied_to_file(self.torrent_id)))   # defined by d.get_tied_to_file - tied_to_file
    tracker_focus       = property(MWT()( lambda self: self.server.d.get_tracker_focus(self.torrent_id)))   # defined by d.get_tracker_focus - tracker_focus
    tracker_numwant     = property( MWT()( lambda self: self.server.d.get_tracker_numwant(self.torrent_id)), lambda self: self.server.d.set_tracker_numwant(self.torrent_id))   # defined by d.get_tracker_numwant - tracker_numwant
    tracker_size        = property(MWT()( lambda self: self.server.d.get_tracker_size(self.torrent_id)))   # defined by d.get_tracker_size - tracker_size
    up_rate             = property(MWT()( lambda self: self.server.d.get_up_rate(self.torrent_id)))   # defined by d.get_up_rate - up_rate
    up_total            = property(MWT()( lambda self: self.server.d.get_up_total(self.torrent_id)))   # defined by d.get_up_total - up_total
    uploads_max         = property( MWT()( lambda self: self.server.d.get_uploads_max(self.torrent_id)), lambda self: self.server.d.set_uploads_max(self.torrent_id))   # defined by d.get_uploads_max - uploads_max
    # defines d.is_active   # defined by d.is_active - is_active
    # defines d.is_hash_checked   # defined by d.is_hash_checked - is_hash_checked
    # defines d.is_hash_checking   # defined by d.is_hash_checking - is_hash_checking
    # defines d.is_multi_file   # defined by d.is_multi_file - is_multi_file
    # defines d.is_open   # defined by d.is_open - is_open
    # defines d.is_private   # defined by d.is_private - is_private
    # defines d.open   # defined by d.open - open
    # defines d.start   # defined by d.start - start
    # defines d.stop   # defined by d.stop - stop
    # defines d.update_priorities   # defined by d.update_priorities - update_priorities
    frozen_path         = property(MWT()( lambda self: self.server.f.get_frozen_path(self.torrent_id)))   # defined by f.get_frozen_path - frozen_path
    is_created          = property(MWT()( lambda self: self.server.f.get_is_created(self.torrent_id)))   # defined by f.get_is_created - is_created
    last_touched        = property(MWT()( lambda self: self.server.f.get_last_touched(self.torrent_id)))   # defined by f.get_last_touched - last_touched
    match_depth_next    = property(MWT()( lambda self: self.server.f.get_match_depth_next(self.torrent_id)))   # defined by f.get_match_depth_next - match_depth_next
    match_depth_prev    = property(MWT()( lambda self: self.server.f.get_match_depth_prev(self.torrent_id)))   # defined by f.get_match_depth_prev - match_depth_prev
    offset              = property(MWT()( lambda self: self.server.f.get_offset(self.torrent_id)))   # defined by f.get_offset - offset
    path                = property(MWT()( lambda self: self.server.f.get_path(self.torrent_id)))   # defined by f.get_path - path
    path_components     = property(MWT()( lambda self: self.server.f.get_path_components(self.torrent_id)))   # defined by f.get_path_components - path_components
    path_depth          = property(MWT()( lambda self: self.server.f.get_path_depth(self.torrent_id)))   # defined by f.get_path_depth - path_depth
    range_first         = property(MWT()( lambda self: self.server.f.get_range_first(self.torrent_id)))   # defined by f.get_range_first - range_first
    range_second        = property(MWT()( lambda self: self.server.f.get_range_second(self.torrent_id)))   # defined by f.get_range_second - range_second
    group               = property(MWT()( lambda self: self.server.t.get_group(self.torrent_id)))   # defined by t.get_group - group
    id                  = property(MWT()( lambda self: self.server.t.get_id(self.torrent_id)))   # defined by t.get_id - id
    min_interval        = property(MWT()( lambda self: self.server.t.get_min_interval(self.torrent_id)))   # defined by t.get_min_interval - min_interval
    normal_interval     = property(MWT()( lambda self: self.server.t.get_normal_interval(self.torrent_id)))   # defined by t.get_normal_interval - normal_interval
    scrape_complete     = property(MWT()( lambda self: self.server.t.get_scrape_complete(self.torrent_id)))   # defined by t.get_scrape_complete - scrape_complete
    scrape_downloaded   = property(MWT()( lambda self: self.server.t.get_scrape_downloaded(self.torrent_id)))   # defined by t.get_scrape_downloaded - scrape_downloaded
    scrape_incomplete   = property(MWT()( lambda self: self.server.t.get_scrape_incomplete(self.torrent_id)))   # defined by t.get_scrape_incomplete - scrape_incomplete
    scrape_time_last    = property(MWT()( lambda self: self.server.t.get_scrape_time_last(self.torrent_id)))   # defined by t.get_scrape_time_last - scrape_time_last
    type                = property(MWT()( lambda self: self.server.t.get_type(self.torrent_id)))   # defined by t.get_type - type
    url                 = property(MWT()( lambda self: self.server.t.get_url(self.torrent_id)))   # defined by t.get_url - url
    # defines t.is_enabled   # defined by t.is_enabled - is_enabled

    def set_enabled(self, v): self.server.t.set_enabled(v)   # defined by t.set_enabled - enabled
class RTorrent(object):
    """ Handles connection and protocol with rtorrent scgi """
    def __init__(self, scgi_address = "http://localhost:5001"):
        self.address = scgi_address
        self._connect() # initalizes self.server
        #lazy_init = self.downloads
        #self._fetch_torrents() # initializes self.torrents

    def _connect(self):
        log.debug("Connecting to %s", self.address)
        self.server = xmlrpclib.ServerProxy(self.address)
        log.info("Connected to %s (%s)", self.address, self.server)

    def _get_torrents(self):
        torrents = {}
        for torrent_id in self.downloads:
            torrents[torrent_id] = Torrent(self.server, torrent_id)
        return torrents

    #def _fetch_downloads(self):
        #log.debug("Fetching downloads from  %s (%s)", self.address, self.server)
        #return self.server.download_list()

    #def get_methods(self):
        #return sorted(self.server.system.listMethods())

    torrents = property(MWT()( _get_torrents))
    downloads = property(MWT()( lambda self: self.server.download_list()))
    methods = property(MWT()( lambda self: self.server.system.listMethods()))

    #@classmethod
    #def get_cmp(cls, *args, **kwargs):
        #def my_cmp(a, b):
            #x, y = a[1].name, b[1].name
            #return cmp(x, y)
        #return my_cmp
    # defines    # defined by  -
    # defines call_download   # defined by call_download - call_download
    # defines cat   # defined by cat - cat
    # defines close_low_diskspace   # defined by close_low_diskspace - close_low_diskspace
    # defines close_on_ratio   # defined by close_on_ratio - close_on_ratio
    # defines close_untied   # defined by close_untied - close_untied
    # defines create_link   # defined by create_link - create_link
    # defines download_list   # defined by download_list - download_list
    # defines enable_trackers   # defined by enable_trackers - enable_trackers
    # defines encoding_list   # defined by encoding_list - encoding_list
    # defines encryption   # defined by encryption - encryption
    # defines execute   # defined by execute - execute
    # defines execute_log   # defined by execute_log - execute_log
    # defines execute_nothrow   # defined by execute_nothrow - execute_nothrow
    # defines execute_raw   # defined by execute_raw - execute_raw
    # defines execute_raw_nothrow   # defined by execute_raw_nothrow - execute_raw_nothrow
    bind                = property( MWT()( lambda self: self.server.get_bind(self.torrent_id)), lambda self: self.server.set_bind(self.torrent_id))   # defined by get_bind - bind
    download_rate       = property( MWT()( lambda self: self.server.get_download_rate(self.torrent_id)), lambda self: self.server.set_download_rate(self.torrent_id))   # defined by get_download_rate - download_rate
    handshake_log       = property( MWT()( lambda self: self.server.get_handshake_log(self.torrent_id)), lambda self: self.server.set_handshake_log(self.torrent_id))   # defined by get_handshake_log - handshake_log
    hash_interval       = property( MWT()( lambda self: self.server.get_hash_interval(self.torrent_id)), lambda self: self.server.set_hash_interval(self.torrent_id))   # defined by get_hash_interval - hash_interval
    hash_max_tries      = property( MWT()( lambda self: self.server.get_hash_max_tries(self.torrent_id)), lambda self: self.server.set_hash_max_tries(self.torrent_id))   # defined by get_hash_max_tries - hash_max_tries
    hash_read_ahead     = property( MWT()( lambda self: self.server.get_hash_read_ahead(self.torrent_id)), lambda self: self.server.set_hash_read_ahead(self.torrent_id))   # defined by get_hash_read_ahead - hash_read_ahead
    http_cacert         = property( MWT()( lambda self: self.server.get_http_cacert(self.torrent_id)), lambda self: self.server.set_http_cacert(self.torrent_id))   # defined by get_http_cacert - http_cacert
    http_capath         = property( MWT()( lambda self: self.server.get_http_capath(self.torrent_id)), lambda self: self.server.set_http_capath(self.torrent_id))   # defined by get_http_capath - http_capath
    http_proxy          = property( MWT()( lambda self: self.server.get_http_proxy(self.torrent_id)), lambda self: self.server.set_http_proxy(self.torrent_id))   # defined by get_http_proxy - http_proxy
    ip                  = property( MWT()( lambda self: self.server.get_ip(self.torrent_id)), lambda self: self.server.set_ip(self.torrent_id))   # defined by get_ip - ip
    key_layout          = property( MWT()( lambda self: self.server.get_key_layout(self.torrent_id)), lambda self: self.server.set_key_layout(self.torrent_id))   # defined by get_key_layout - key_layout
    max_downloads_div   = property( MWT()( lambda self: self.server.get_max_downloads_div(self.torrent_id)), lambda self: self.server.set_max_downloads_div(self.torrent_id))   # defined by get_max_downloads_div - max_downloads_div
    max_downloads_global = property( MWT()( lambda self: self.server.get_max_downloads_global(self.torrent_id)), lambda self: self.server.set_max_downloads_global(self.torrent_id))   # defined by get_max_downloads_global - max_downloads_global
    max_memory_usage    = property( MWT()( lambda self: self.server.get_max_memory_usage(self.torrent_id)), lambda self: self.server.set_max_memory_usage(self.torrent_id))   # defined by get_max_memory_usage - max_memory_usage
    max_open_files      = property( MWT()( lambda self: self.server.get_max_open_files(self.torrent_id)), lambda self: self.server.set_max_open_files(self.torrent_id))   # defined by get_max_open_files - max_open_files
    max_open_http       = property( MWT()( lambda self: self.server.get_max_open_http(self.torrent_id)), lambda self: self.server.set_max_open_http(self.torrent_id))   # defined by get_max_open_http - max_open_http
    max_open_sockets    = property( MWT()( lambda self: self.server.get_max_open_sockets(self.torrent_id)), lambda self: self.server.set_max_open_sockets(self.torrent_id))   # defined by get_max_open_sockets - max_open_sockets
    max_peers_seed      = property( MWT()( lambda self: self.server.get_max_peers_seed(self.torrent_id)), lambda self: self.server.set_max_peers_seed(self.torrent_id))   # defined by get_max_peers_seed - max_peers_seed
    max_uploads_div     = property( MWT()( lambda self: self.server.get_max_uploads_div(self.torrent_id)), lambda self: self.server.set_max_uploads_div(self.torrent_id))   # defined by get_max_uploads_div - max_uploads_div
    max_uploads_global  = property( MWT()( lambda self: self.server.get_max_uploads_global(self.torrent_id)), lambda self: self.server.set_max_uploads_global(self.torrent_id))   # defined by get_max_uploads_global - max_uploads_global
    memory_usage        = property(MWT()( lambda self: self.server.get_memory_usage(self.torrent_id)))   # defined by get_memory_usage - memory_usage
    min_peers_seed      = property( MWT()( lambda self: self.server.get_min_peers_seed(self.torrent_id)), lambda self: self.server.set_min_peers_seed(self.torrent_id))   # defined by get_min_peers_seed - min_peers_seed
    port_open           = property( MWT()( lambda self: self.server.get_port_open(self.torrent_id)), lambda self: self.server.set_port_open(self.torrent_id))   # defined by get_port_open - port_open
    port_random         = property( MWT()( lambda self: self.server.get_port_random(self.torrent_id)), lambda self: self.server.set_port_random(self.torrent_id))   # defined by get_port_random - port_random
    port_range          = property( MWT()( lambda self: self.server.get_port_range(self.torrent_id)), lambda self: self.server.set_port_range(self.torrent_id))   # defined by get_port_range - port_range
    preload_min_size    = property( MWT()( lambda self: self.server.get_preload_min_size(self.torrent_id)), lambda self: self.server.set_preload_min_size(self.torrent_id))   # defined by get_preload_min_size - preload_min_size
    preload_required_rate = property( MWT()( lambda self: self.server.get_preload_required_rate(self.torrent_id)), lambda self: self.server.set_preload_required_rate(self.torrent_id))   # defined by get_preload_required_rate - preload_required_rate
    preload_type        = property( MWT()( lambda self: self.server.get_preload_type(self.torrent_id)), lambda self: self.server.set_preload_type(self.torrent_id))   # defined by get_preload_type - preload_type
    proxy_address       = property( MWT()( lambda self: self.server.get_proxy_address(self.torrent_id)), lambda self: self.server.set_proxy_address(self.torrent_id))   # defined by get_proxy_address - proxy_address
    receive_buffer_size = property( MWT()( lambda self: self.server.get_receive_buffer_size(self.torrent_id)), lambda self: self.server.set_receive_buffer_size(self.torrent_id))   # defined by get_receive_buffer_size - receive_buffer_size
    safe_free_diskspace = property(MWT()( lambda self: self.server.get_safe_free_diskspace(self.torrent_id)))   # defined by get_safe_free_diskspace - safe_free_diskspace
    safe_sync           = property( MWT()( lambda self: self.server.get_safe_sync(self.torrent_id)), lambda self: self.server.set_safe_sync(self.torrent_id))   # defined by get_safe_sync - safe_sync
    scgi_dont_route     = property( MWT()( lambda self: self.server.get_scgi_dont_route(self.torrent_id)), lambda self: self.server.set_scgi_dont_route(self.torrent_id))   # defined by get_scgi_dont_route - scgi_dont_route
    send_buffer_size    = property( MWT()( lambda self: self.server.get_send_buffer_size(self.torrent_id)), lambda self: self.server.set_send_buffer_size(self.torrent_id))   # defined by get_send_buffer_size - send_buffer_size
    session             = property( MWT()( lambda self: self.server.get_session(self.torrent_id)), lambda self: self.server.set_session(self.torrent_id))   # defined by get_session - session
    session_lock        = property( MWT()( lambda self: self.server.get_session_lock(self.torrent_id)), lambda self: self.server.set_session_lock(self.torrent_id))   # defined by get_session_lock - session_lock
    session_on_completion = property( MWT()( lambda self: self.server.get_session_on_completion(self.torrent_id)), lambda self: self.server.set_session_on_completion(self.torrent_id))   # defined by get_session_on_completion - session_on_completion
    split_file_size     = property( MWT()( lambda self: self.server.get_split_file_size(self.torrent_id)), lambda self: self.server.set_split_file_size(self.torrent_id))   # defined by get_split_file_size - split_file_size
    split_suffix        = property( MWT()( lambda self: self.server.get_split_suffix(self.torrent_id)), lambda self: self.server.set_split_suffix(self.torrent_id))   # defined by get_split_suffix - split_suffix
    stats_not_preloaded = property(MWT()( lambda self: self.server.get_stats_not_preloaded(self.torrent_id)))   # defined by get_stats_not_preloaded - stats_not_preloaded
    stats_preloaded     = property(MWT()( lambda self: self.server.get_stats_preloaded(self.torrent_id)))   # defined by get_stats_preloaded - stats_preloaded
    timeout_safe_sync   = property( MWT()( lambda self: self.server.get_timeout_safe_sync(self.torrent_id)), lambda self: self.server.set_timeout_safe_sync(self.torrent_id))   # defined by get_timeout_safe_sync - timeout_safe_sync
    timeout_sync        = property( MWT()( lambda self: self.server.get_timeout_sync(self.torrent_id)), lambda self: self.server.set_timeout_sync(self.torrent_id))   # defined by get_timeout_sync - timeout_sync
    tracker_dump        = property( MWT()( lambda self: self.server.get_tracker_dump(self.torrent_id)), lambda self: self.server.set_tracker_dump(self.torrent_id))   # defined by get_tracker_dump - tracker_dump
    upload_rate         = property( MWT()( lambda self: self.server.get_upload_rate(self.torrent_id)), lambda self: self.server.set_upload_rate(self.torrent_id))   # defined by get_upload_rate - upload_rate
    use_udp_trackers    = property( MWT()( lambda self: self.server.get_use_udp_trackers(self.torrent_id)), lambda self: self.server.set_use_udp_trackers(self.torrent_id))   # defined by get_use_udp_trackers - use_udp_trackers
    # defines if   # defined by if - if
    # defines import   # defined by import - import
    # defines load   # defined by load - load
    # defines load_raw   # defined by load_raw - load_raw
    # defines load_raw_start   # defined by load_raw_start - load_raw_start
    # defines load_raw_verbose   # defined by load_raw_verbose - load_raw_verbose
    # defines load_start   # defined by load_start - load_start
    # defines load_start_verbose   # defined by load_start_verbose - load_start_verbose
    # defines load_verbose   # defined by load_verbose - load_verbose
    # defines on_close   # defined by on_close - on_close
    # defines on_erase   # defined by on_erase - on_erase
    # defines on_finished   # defined by on_finished - on_finished
    # defines on_hash_done   # defined by on_hash_done - on_hash_done
    # defines on_hash_queued   # defined by on_hash_queued - on_hash_queued
    # defines on_hash_removed   # defined by on_hash_removed - on_hash_removed
    # defines on_insert   # defined by on_insert - on_insert
    # defines on_open   # defined by on_open - on_open
    # defines on_start   # defined by on_start - on_start
    # defines on_stop   # defined by on_stop - on_stop
    # defines print   # defined by print - print
    # defines remove_untied   # defined by remove_untied - remove_untied
    # defines scgi_local   # defined by scgi_local - scgi_local
    # defines scgi_port   # defined by scgi_port - scgi_port
    # defines schedule   # defined by schedule - schedule
    # defines schedule_remove   # defined by schedule_remove - schedule_remove
    # defines session_save   # defined by session_save - session_save
    # defines start_tied   # defined by start_tied - start_tied
    # defines stop_on_ratio   # defined by stop_on_ratio - stop_on_ratio
    # defines stop_untied   # defined by stop_untied - stop_untied
    # defines to_date   # defined by to_date - to_date
    # defines to_elapsed_time   # defined by to_elapsed_time - to_elapsed_time
    # defines to_kb   # defined by to_kb - to_kb
    # defines to_mb   # defined by to_mb - to_mb
    # defines to_time   # defined by to_time - to_time
    # defines to_xb   # defined by to_xb - to_xb
    # defines tos   # defined by tos - tos
    # defines try_import   # defined by try_import - try_import
    # defines view_add   # defined by view_add - view_add
    # defines view_filter   # defined by view_filter - view_filter
    # defines view_filter_on   # defined by view_filter_on - view_filter_on
    # defines view_list   # defined by view_list - view_list
    # defines view_sort   # defined by view_sort - view_sort
    # defines view_sort_current   # defined by view_sort_current - view_sort_current
    # defines view_sort_new   # defined by view_sort_new - view_sort_new
    # defines xmlrpc_dialect   # defined by xmlrpc_dialect - xmlrpc_dialect
    # defines system.client_version   # defined by system.client_version - client_version
    cwd                 = property( MWT()( lambda self: self.server.system.get_cwd(self.torrent_id)), lambda self: self.server.system.set_cwd(self.torrent_id))   # defined by system.get_cwd - cwd
    # defines system.hostname   # defined by system.hostname - hostname
    # defines system.library_version   # defined by system.library_version - library_version
    # defines system.listMethods   # defined by system.listMethods - listMethods
    # defines system.methodHelp   # defined by system.methodHelp - methodHelp
    # defines system.methodSignature   # defined by system.methodSignature - methodSignature
    # defines system.multicall   # defined by system.multicall - multicall
    # defines system.pid   # defined by system.pid - pid
    def set_umask(self, v): self.server.system.set_umask(v)   # defined by system.set_umask - umask
    # defines system.shutdown   # defined by system.shutdown - shutdown

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG, format='%(levelname)s %(message)s')

    rt = RTorrent()
    for x in rt.torrents.values():
            print '===', x.name,x.complete #,x.is_open
    for x in rt.methods: print x



